home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / TransSkel Pascal 2.5 / TransSkel / ManyWind ƒ / ManyWind.p < prev    next >
Encoding:
Text File  |  1994-12-12  |  7.4 KB  |  281 lines  |  [TEXT/PJMM]

  1. {    ManyWind TransSkel demonstration}
  2.  
  3. {    This application allows up to twenty windows to be created at once,}
  4. {    with the New item under the File menu.  The name of each window}
  5. {    appears under the Windows menu (which is not created until at least}
  6. {    one window exists).  Selecting the window name from the Windows menu}
  7. {    brings the window to the front.  For every window created, Skel is}
  8. {    told to create a new handler.  If the window's close box is clicked,}
  9. {    the handler removes the window name from the Windows menu, disposes}
  10. {    of the window, and removes itself from the window handler list.  If}
  11. {    the window was the last window, the Windows menu handler removes}
  12. {    itself from the menu handler list.}
  13.  
  14. {    When the first window is created, a Color menu also appears.  This}
  15. {    allows the color of the content region of the frontmost window to}
  16. {    be changed.  It goes away when the last window is closed.}
  17.  
  18. {    To quit, select Quit from the File menu or type command-Q.}
  19.  
  20. {    ManyWind demonstrates dynamic window and menu creation and disposal.}
  21. {    It also shows how handler procedures may be shared among handlers}
  22. {    for different windows.}
  23.  
  24. {    The project should include this file, TransSkel.p (or a library }
  25. {    built from TransSkel.p), Runtime.lib and Interface.lib .}
  26.  
  27. {    28 June 1986        Paul DuBois}
  28. {    7 January 1987 Owen Hartnett, Ωhm Software Co.    }
  29. {     6 June 1988 OH, changes for version 2.00 }
  30.  
  31. program ManyWind;
  32.  
  33.     uses
  34. {$IFC UNDEFINED THINK_PASCAL}
  35.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf, 
  36. {$ENDC}
  37.         TransSkel;
  38.  
  39.     const
  40.         maxWind = 20;    { maximum number of windows existing at once }
  41.  
  42.         aMenuNum = 1;    { Apple menu }
  43.         fMenuNum = 2;        { File menu }
  44.         wMenuNum = 3;        { Windows menu }
  45.         cMenuNum = 4;        { Color menu }
  46.  
  47.         new = 1;
  48.         quit = 3;
  49.  
  50.         cWhite = 1;
  51.         cLtGray = 2;
  52.         cGray = 3;
  53.         cDkGray = 4;
  54.         cBlack = 5;
  55.  
  56.     var
  57.         fileMenu, windowMenu, colorMenu: MenuHandle;
  58.  
  59.         windCount: integer;    { number of currently existing windows }
  60.         windNum: longint;    { id of last window created }
  61.  
  62.         dummy: Boolean;        { may be used for memory management }
  63.  
  64.     procedure MakeWindow;
  65.     forward;
  66.     procedure DoWClose;
  67.     forward;
  68.  
  69.     procedure DoWUpdate;
  70.  
  71.         var
  72.             thePort: GrafPtr;
  73.  
  74.     begin
  75.         GetPort(thePort);
  76.         EraseRect(thePort^.portRect);    { repaint w/background pattern }
  77.     end;
  78.  
  79.     procedure DoMClobber (theMenu: MenuHandle);
  80.  
  81.     begin
  82.         DisposeMenu(theMenu);
  83.     end;
  84.  
  85.     procedure DoFileMenu (item: integer);
  86.  
  87.     begin
  88.         case item of
  89.             quit: 
  90.                 SkelWhoa;        { tell SkelMain to quit }
  91.             new: 
  92.                 MakeWindow;    { make a new window }
  93.         end;
  94.     end;
  95.  
  96. {    Dispose of window.  Skel makes sure the port is pointing to the}
  97. {    appropriate window, so this procedure can determine which window}
  98. {    is to be disposed, of without being told explicitly.}
  99.  
  100.  
  101.     procedure DoWClobber;
  102.  
  103.         var
  104.             thePort: GrafPtr;
  105.  
  106.     begin
  107.         GetPort(thePort);                { grafport of window to dispose of }
  108.         DisposeWindow(WindowPtr(thePort));
  109.     end;
  110.  
  111. {    Change the background pattern of the frontmost window.  Ignore}
  112. {    if the front window is a DA window.}
  113.  
  114.     procedure DoColorMenu (item: integer);
  115.  
  116.         var
  117.             w: WindowPeek;
  118.             w2: WindowPtr;
  119.  
  120.     begin
  121.         w := WindowPeek(FrontWindow);
  122. {•  SetPort(WindowPtr(w));                {*** Fixed bug in original windows    •]}
  123.         if w^.windowKind >= 0 then             { front is not DA window }
  124.             begin
  125.                 case item of
  126. {$IFC UNDEFINED THINK_PASCAL}
  127.                     cWhite: 
  128.                         BackPat(qd.white);
  129.                     cLtGray: 
  130.                         BackPat(qd.ltGray);
  131.                     cGray: 
  132.                         BackPat(qd.gray);
  133.                     cDkGray: 
  134.                         BackPat(qd.dkGray);
  135.                     cBlack: 
  136.                         BackPat(qd.black);
  137. {$ELSEC}
  138.                     cWhite: 
  139.                         BackPat(white);
  140.                     cLtGray: 
  141.                         BackPat(ltGray);
  142.                     cGray: 
  143.                         BackPat(gray);
  144.                     cDkGray: 
  145.                         BackPat(dkGray);
  146.                     cBlack: 
  147.                         BackPat(black);
  148. {$ENDC}
  149.                 end;
  150.                 w2 := WindowPtr(w);
  151.                 EraseRect(w2^.portRect);
  152.             end;
  153.     end;
  154.  
  155.  
  156.     procedure DoWindowMenu (item: integer);
  157.  
  158.         var
  159.             iTitle, wTitle: Str255;
  160.             w: WindowPeek;
  161.  
  162.     begin
  163.         GetItem(windowMenu, item, iTitle);     { get window name }
  164.         w := WindowPeek(FrontWindow);
  165.         while w <> nil do
  166.             begin
  167.                 GetWTitle(WindowPtr(w), wTitle);
  168.                 if EqualString(iTitle, wTitle, false, true) then
  169.                     begin
  170.                         SelectWindow(WindowPtr(w));
  171.                         w := nil;
  172.                     end;
  173.                 if w <> nil then
  174.                     w := w^.nextWindow;
  175.             end;
  176.     end;
  177.  
  178. {    Make new window.  Locate at (100, 100) if no other windows, else}
  179. {    offset slightly from front window.  The window title is the next}
  180. {    window number (1, 2, 3, ...).  If this is the first window, create}
  181. {    the Windows and Color menus.  Add the window title as the last item}
  182. {    of the Windows menu.}
  183.  
  184. {    If the maximum window count has been reached, disable New in the}
  185. {    File menu.}
  186.  
  187.     procedure MakeWindow;
  188.  
  189.         var
  190.             w: WindowPtr;
  191.             r, r2: Rect;
  192.             s: Str255;
  193.  
  194.     begin
  195.         SetRect(r, 0, 0, 200, 150);
  196.         w := FrontWindow;
  197.         if w = nil then
  198.             OffsetRect(r, 100, 100)
  199.         else
  200.             begin
  201.                 r2 := w^.portBits.bounds;
  202.                 OffSetRect(r, 20 - r2.left, 20 - r2.top);
  203.                 if (r.left > 480) or (r.top > 300) then    { keep on screen }
  204.                     OffsetRect(r, 40 - r.left, 40 - r.top);
  205.             end;
  206.         WindNum := windnum + 1;
  207.         NumToString(windNum, s);
  208.         w := NewWindow(nil, r, s, true, documentProc, WindowPtr(-1), true, 0);
  209.         dummy := SkelWindow(w, nil, nil, @DoWUpdate, nil, @DoWClose, @DoWclobber, nil, false);
  210.         windCount := windCount + 1;
  211.         if windCount - 1 = 0 then     { if first window, create new menus }
  212.             begin
  213.                 colorMenu := NewMenu(cMenuNum, 'Color');
  214.                 AppendMenu(colorMenu, 'White;Light Gray;Gray;Dark Gray;Black');
  215.                 dummy := SkelMenu(colorMenu, @DoColorMenu, @DoMClobber, false);
  216.                 windowMenu := NewMenu(wMenuNum, 'Windows');
  217.                 dummy := SkelMenu(windowMenu, @DoWindowMenu, @DoMClobber, true);
  218.             end;
  219.         AppendMenu(windowMenu, s);
  220.         if windCount = maxWind then
  221.             DisableItem(fileMenu, new);
  222.     end;
  223.  
  224. {    Mouse was clicked in close box.  Remove the window handler (which}
  225. {    causes the window to be disposed of), and delete the window title}
  226. {    from the Windows menu.  If the window was the last one, delete the}
  227. {    Windows and Color menus entirely.}
  228.  
  229. {    Skel makes sure the port is pointing to the appropriate window, so}
  230. {    this procedure can determine which window had its close box clicked,}
  231. {    without being told explicitly.}
  232.  
  233.     procedure DoWClose;
  234.  
  235.         var
  236.             thePort: GrafPtr;
  237.             m: MenuHandle;
  238.             i, mItems: integer;
  239.             iTitle, wTitle: Str255;
  240.  
  241.     begin
  242.         GetPort(thePort);            { grafport of window to be closed }
  243.         GetWTitle(WindowPtr(thePort), wTitle);
  244.         SkelRmveWind(WindowPtr(thePort));
  245.         windCount := windCount - 1;
  246.         if windCount = 0 then
  247.             begin
  248.                 SkelRmveMenu(windowMenu);    { last window - clobber menus }
  249.                 SkelRmveMenu(colorMenu);
  250.             end
  251.         else
  252.             begin    { just take out of menu }
  253.                 m := NewMenu(wMenuNum, 'Windows');
  254.                 mItems := CountMItems(windowMenu);
  255.                 for i := 1 to mItems do
  256.                     begin
  257.                         GetItem(windowMenu, i, iTitle);
  258.                         if not EqualString(iTitle, wTitle, false, true) then
  259.                             AppendMenu(m, iTitle);
  260.                     end;
  261.                 SkelRmveMenu(windowMenu);    { remove old Windows menu }
  262.                 windowMenu := m;                { and install new one }
  263.                 dummy := SkelMenu(windowMenu, @DoWindowMenu, @DoMClobber, true);
  264.             end;
  265.         EnableItem(fileMenu, new);    { can always create at least one more now }
  266.     end;
  267.  
  268.  
  269. begin
  270.  
  271.     WindCount := 0;
  272.     WindNum := 0;
  273.     SkelInit(6, nil);                                            { initialize }
  274.     SkelApple('(About ManyWind…', nil);                                { handle desk accessories }
  275.     fileMenu := NewMenu(fMenuNum, 'File');        { make File menu handler }
  276.     AppendMenu(fileMenu, 'New/N;(-;Quit/Q');
  277.     SkelGrowBounds(nil, 50, 10, 500, 300);
  278.     dummy := SkelMenu(fileMenu, @DoFileMenu, @DoMClobber, true);
  279.     SkelMain;                                        { loop 'til Quit selected }
  280.     SkelClobber;                                    { clean up }
  281. end.